package com.xie.o2o.controller;

import com.google.common.collect.Maps;
import com.xie.o2o.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @create: xie
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger log= LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map<String,Object> handler(Exception e){
        Map<String,Object> map= Maps.newHashMap();
        map.put("success",false);
        if(e instanceof RuntimeException){
            log.error(LogUtil.getMsg(e));
            map.put("errMsg",e.getMessage());
        }else {
            log.error("系统未知异常:");
            log.error(LogUtil.getMsg(e));
            map.put("errMsg","未知异常，请联系管理员");
        }
        return map;
    }
}
